package org.kl.bf.repository.basic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.kl.bf.entity.basic.BasicFile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Component;

@Component
public class NotifyFileDao {
	private JdbcTemplate jdbcTemp;

	@Resource
	public void setDataSource(DataSource dataSource) {
		jdbcTemp = new JdbcTemplate(dataSource);
	}

	public List<BasicFile> queryNotifyFileForNotify(String notifyId) {
		String sql = "SELECT a.* FROM basic_notify_file a LEFT JOIN basic_notify_join_file na ON na.notify_file_id = a.id WHERE na.notify_id = '"
				+ notifyId + "' ";
		final List<BasicFile> result = new ArrayList<BasicFile>();
		this.jdbcTemp.query(sql, new RowCallbackHandler() {
			public void processRow(ResultSet rs) throws SQLException {
				BasicFile a = new BasicFile();
				a.setId(rs.getString("d"));
				a.setDownloader(rs.getString("downloader"));
				a.setFileName(rs.getString("file_name"));
				a.setFilePath(rs.getString("file_path"));
				a.setFileSize(rs.getString("file_size"));
				a.setFileType(rs.getString("file_type"));
				a.setLastDownloadTime(rs.getTimestamp("last_download_time"));
				a.setUploadTime(rs.getTimestamp("upload_time"));
				a.setUploader(rs.getString("uploader"));
				result.add(a);
			}
		});
		return result;
	}
}
